#include "VIN_BitMapFont.h"
#include "tinyxml/tinyxml.h"
#include "sgd wrappers/CSGD_TextureManager.h"

VIN_BitMapFont::VIN_BitMapFont()
{

}
VIN_BitMapFont::~VIN_BitMapFont()
{

}

void VIN_BitMapFont::Startup()
{
	Load("resource/fonts/MenuFont.fnt", 0);
	Load("resource/fonts/GameFont.fnt", 1);
}

void VIN_BitMapFont::Render(const char* TextToPrint, int posx, int posy, float scale, DWORD color, int GameFont)
{
	Render( TextToPrint, posx, posy, scale, scale, color, GameFont);
}

void VIN_BitMapFont::Render(const char* TextToPrint, int posx, int posy, float scalex, float scaley, DWORD color, int GameFont)
{
	int originalx = posx;
	int tempPosX = posx;

	int len = strlen(TextToPrint);
	Font print[100];
	for(int i = 0; i < len; i++)
	{
		char ch = TextToPrint[i];

		for(int j = 0; j < 100; j++)
		{
			if(GameFont == 0)
			{
				if(ch == m_nMenuFont[j].id)
				{
					print[i] = m_nMenuFont[j];
					break;
				}
			}
			else if(GameFont == 1)
			{
				if(ch == m_nGameFont[j].id)
				{
					print[i] = m_nGameFont[j];
					break;
				}
			}
		}
		RECT Letter;
		SetRect(&Letter, print[i].x, print[i].y,print[i].x + print[i].width, print[i].y + print[i].height);
		if(GameFont == 0)
			CSGD_TextureManager::GetInstance()->Draw( MenuFontID, (int)(tempPosX + print[i].xoffset * scalex), (int)(posy + print[i].yoffset * scaley), scalex, scaley, &Letter ,0,0,0,color); /**/
		else if(GameFont == 1)
			CSGD_TextureManager::GetInstance()->Draw( GameFontID, (int)(tempPosX + print[i].xoffset * scalex), (int)(posy + print[i].yoffset * scaley), scalex, scaley, &Letter ,0,0,0,color); /**/
		tempPosX += (int)(print[i].xadvance * scalex);	
	}
}

// pass in 1 for a Gamefont load
void VIN_BitMapFont::Load(const char* filename, int GameFont)
{
	//Font tempfont;
	TiXmlDocument doc;

	doc.LoadFile(filename);

	TiXmlElement* root = doc.RootElement();

	TiXmlElement* file = root->FirstChildElement("pages");
	TiXmlElement* xfilepath = file->FirstChildElement("page");
	string filepath = xfilepath->Attribute("file");
	TiXmlElement* chars = root->FirstChildElement("chars");

	int count = atoi(chars->Attribute("count"));
	TiXmlElement* numchars = chars->FirstChildElement("char");

	if(GameFont == 0)
	{
		MenuFontID = CSGD_TextureManager::GetInstance()->LoadTexture(filepath.c_str());

		for(int i = 0; i < count; i++)
		{
			/*TiXmlAttribute* id = */numchars->Attribute("id", &m_nMenuFont[i].id);
			//m_nMenuFont[i].id = atoi(id->Value());

			numchars->Attribute("x", &m_nMenuFont[i].x);
			//m_nMenuFont[i].x = atoi(x->Value());

			numchars->Attribute("y", &m_nMenuFont[i].y);
			//m_nMenuFont[i].y = atoi(y->Value());

			numchars->Attribute("width", &m_nMenuFont[i].width);
			//m_nMenuFont[i].width = atoi(width->Value());

			numchars->Attribute("height", &m_nMenuFont[i].height);
			//m_nMenuFont[i].height = atoi(height->Value());

			numchars->Attribute("xoffset", &m_nMenuFont[i].xoffset);
			//m_nMenuFont[i].xoffset = atoi(xoffset->Value());

			numchars->Attribute("yoffset", &m_nMenuFont[i].yoffset);
			//m_nMenuFont[i].yoffset = atoi(yoffset->Value());

			numchars->Attribute("xadvance", &m_nMenuFont[i].xadvance);
			//m_nMenuFont[i].xadvance = atoi(xadvance->Value());

			numchars = numchars->NextSiblingElement("char");
		}
	}
	else if(GameFont == 1)
	{
		GameFontID = CSGD_TextureManager::GetInstance()->LoadTexture(filepath.c_str());

		for(int i = 0; i < count; i++)
		{
			/*TiXmlAttribute* id = */numchars->Attribute("id", &m_nGameFont[i].id);
			//m_nGameFont[i].id = atoi(id->Value());

			numchars->Attribute("x", &m_nGameFont[i].x);
			//m_nGameFont[i].x = atoi(x->Value());

			numchars->Attribute("y", &m_nGameFont[i].y);
			//m_nGameFont[i].y = atoi(y->Value());

			numchars->Attribute("width", &m_nGameFont[i].width);
			//m_nGameFont[i].width = atoi(width->Value());

			numchars->Attribute("height", &m_nGameFont[i].height);
			//m_nGameFont[i].height = atoi(height->Value());

			numchars->Attribute("xoffset", &m_nGameFont[i].xoffset);
			//m_nGameFont[i].xoffset = atoi(xoffset->Value());

			numchars->Attribute("yoffset", &m_nGameFont[i].yoffset);
			//m_nGameFont[i].yoffset = atoi(yoffset->Value());

			numchars->Attribute("xadvance", &m_nGameFont[i].xadvance);
			//m_nGameFont[i].xadvance = atoi(xadvance->Value());

			numchars = numchars->NextSiblingElement("char");
		}
	}
}